package com.fat.controller;

import com.fat.pojo.Article;
import com.fat.pojo.Result;
import com.fat.pojo.vo.PageBean;
import com.fat.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 文章控制器
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 新增文章
     */
    @PostMapping
    public Result add(@RequestBody @Validated(Article.Add.class) Article article) {
        return articleService.add(article);
    }

    /**
     * 文章列表(条件分页)
     */
    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum,
                                 Integer pageSize,
                                 @RequestParam(required = false) Integer categoryId,
                                 @RequestParam(required = false) String state) {
        return articleService.list(pageNum,pageSize,categoryId,state);
    }

    /**
     * 获取文章详情
     */
    @GetMapping("/detail")
    public Result get(Integer id) {
        return articleService.get(id);
    }

    /**
     * 更新文章
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Article.Update.class) Article article) {
        return articleService.update(article);
    }

    /**
     * 删除文章
     */
    @DeleteMapping
    public Result delete(Integer id) {
        return articleService.delete(id);
    }
}
